from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from product.models import Product
from product.serializers import ProductModelSerializer
from utils.pagination import TenItemPerPagePagination


class ProductViewSet(ModelViewSet):
    queryset = Product.objects.filter(is_delete=False).order_by("-c_time")
    serializer_class = ProductModelSerializer
    pagination_class = TenItemPerPagePagination

    @action(methods=['get'], detail=False)
    def category_list(self, request):
        """
        返回类别列表
        """
        category_list = Product.objects.values_list("category", flat=True).distinct()
        category_list = [i.strip() for i in category_list if i != "" and i != None]
        return Response(data=category_list)

    @action(methods=['get'], detail=False)
    def brand_list(self, request):
        """
        返回品牌列表
        """
        brand_list = Product.objects.values_list("brand", flat=True).distinct()
        brand_list = [i.strip() for i in brand_list if i != "" and i != None]
        return Response(data=brand_list)
